window.Composer = {
    doCompose: async function () {
        var music = {
            name: `${this.createName()}`,
            speed: 100,
            tracks: []
        };
        for (var lenCompose = 0; lenCompose < Utils.random(1, 3); lenCompose++) {
            music.tracks.push(this.createTrack(music));
        }
        console.log(music);
        return music;
    },
    createTrack: function (music) {
        var notes = [];
        for (var lenTrack = 0; lenTrack < Utils.random(1, 7); lenTrack++) {
            this.createSection(music, notes);
        }
        return {
            step: Utils.randomArray(['D', 'C']),
            notes: notes
        };
    },
    createSection: function (music, notes, depth) {
        if (depth == undefined) depth = Utils.random(4, 9);
        if (depth != 0) {
            var tNotes = [];
            this.createSection(music, tNotes, depth - 1);
            this.randomChange(tNotes);
            Utils.pushAll(tNotes, notes);
        } else {
            var sectionLength = Utils.random(0, 3) == 0 ? Utils.random(13, 16) : Utils.random(3, 13);
            for (var lenSection = 0; lenSection < sectionLength; lenSection++) {
                notes.push([this.createNote(), this.createTime()]);
            }
        }
    },
    createNote: function () {
        var c = Utils.random(1, 7).toString();
        if (Utils.random() == 0) {
            var d = Utils.randomArray(['<', '>']);
            for (var lenNote = 0; lenNote < Utils.random(1, 2); lenNote++) {
                c += d;
            }
        }
        return c;
    },
    createTime: function () {
        return Utils.random(0, 4) == 1 ?
            Utils.randomArray(Utils.random(0, 3) == 2 ? [0.5, 1.5, 2] : [0.25, 0.75, 0.5]) : 1;
    },
    randomChange: function (notes) {
        if (notes.length < 3) return;
        for (var lenChange = 0; lenChange < (Utils.random(0, 3) == 1 ? 2 : 1); lenChange++) {
            var position = notes.length - Utils.random(1, 3);
            notes[position][0] = this.createNote();
        }
    },
    createName: (function () {
        var r = function self() {
            return Utils.randomArray(r.formats)
                .replace('{N}', Utils.randomArray(r.nouns));
        }
        r.formats = [
            '看啊,{N}',
            '{N}',
            '那不是{N}?'
        ];
        r.nouns = [
            '小小的星星', '亮闪闪的星星', '一堆作业', '一群人', '一些扇子', '一把扇子', '一个笔记本'
        ]
        return r;
    })()
}